//
//  UserTabBarViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/18.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "UserTabBarViewController.h"
#import "LoginViewController.h"
#import "MakeAnAppointmentToDoLaundryViewController.h"
//#import "MyTabBar.h"

@interface UserTabBarViewController ()<UITabBarControllerDelegate>

//@property (nonatomic,strong) MyTabBar *tabBar;

@end

@implementation UserTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -3)];
    
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:getColor(@"303030")} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:getColor(mainColor)} forState:UIControlStateSelected];
    
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, 20);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    
    [self setTabBarItemWithIndex:0
                  nomalImageName:@"home_nor"
               selectedImageName:@"home_sel"
                           title:@"首页"];
    [self setTabBarItemWithIndex:1
                  nomalImageName:@"mall_nor"
               selectedImageName:@"mall_sel"
                           title:@"商城"];
    [self setTabBarItemWithIndex:2
                  nomalImageName:@"TheOrder_nor"
               selectedImageName:@"TheOrder_sel"
                           title:@"订单"];
    [self setTabBarItemWithIndex:3
                  nomalImageName:@"my_nor"
               selectedImageName:@"my_sel"
                           title:@"我的"];
    
    
//    self.tabBar = [[MyTabBar alloc] init];
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];
    //取消tabBar的透明效果
    self.tabBar.translucent = NO;
//    self.tabBar.myDelegate = self;
    // KVC：如果要修系统的某些属性，但被设为readOnly，就是用KVC，即setValue：forKey：。
    UIImageView *titleImageView = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 60 / WIDTH_5S_SCALE) / 2, - 25 / WIDTH_5S_SCALE, 60 / WIDTH_5S_SCALE, 60 / WIDTH_5S_SCALE)];
    UILabel *titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60 / WIDTH_5S_SCALE, 60 / WIDTH_5S_SCALE)];
    titleLabel.font = [UIFont systemFontOfSize:12];
    titleLabel.textColor = getColor(@"ff5300");
    titleLabel.text = @"预约\n洗衣";
    titleLabel.numberOfLines = 0;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [titleImageView addSubview:titleLabel];
    
    titleImageView.contentMode = UIViewContentModeScaleAspectFit;
    titleImageView.image = [UIImage imageNamed:@"root_tabbar"];
    titleImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleImageViewClick)];
    [titleImageView addGestureRecognizer:tap];
    [self.tabBar addSubview:titleImageView];
//    [self setValue:self.tabBar forKey:@"tabBar"];
    
//    self.bgView = [UIView new];
//    self.bgView.backgroundColor = [UIColor whiteColor];
//    self.bgView.layer.masksToBounds = YES;
//    self.bgView.layer.cornerRadius = 4;
//    self.bgView.size = CGSizeMake(40, 40);
    self.tabBar.layer.shadowColor = [UIColor blackColor].CGColor;
    self.tabBar.layer.shadowRadius= 2;
    self.tabBar.layer.shadowOpacity = .5;
}


/**
 *  设置item图片和文字
 *
 *  @param index             索引
 *  @param nomalImageName    未选择图片
 *  @param selectedImageName 选中图片
 *  @param title             显示文字
 */
- (void) setTabBarItemWithIndex:(NSInteger)index nomalImageName:(NSString*)nomalImageName selectedImageName:(NSString*)selectedImageName title:(NSString *)title{
    UITabBarItem *item = self.tabBar.items[index];
    //设置常规图片
    if (nomalImageName) {
        UIImage *imageNomal = [[UIImage imageNamed:nomalImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item.image = imageNomal;
    }
    //设置选中图片
    UIImage *imageSelected = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item.selectedImage = imageSelected;
    item.title = title;
    
}

//- (void)tabBarDidClickPlusButton:(MyTabBar *)tabBar{
//    
//}

- (void)titleImageViewClick{
    NSLog(@"预约洗衣");
   [[NSNotificationCenter defaultCenter] postNotificationName:@"预约洗衣" object:nil userInfo:nil];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
//    NSUInteger i = [tabBarController.viewControllers indexOfObject:viewController];
//    if (i==2) {
//        self.tabBar.plusBtn.selected = YES;
//        self.tabBar.titleLabel.textColor = getColor(mainColor);
//    }else{
//        self.tabBar.plusBtn.selected = NO;
//        self.tabBar.titleLabel.textColor = getColor(@"303030");
//    }
    return YES;
}

@end
